﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MoneyChangeLog
    {
        public int Add(Chain.Model.MoneyChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MoneyChangeLog(");
            builder.Append("MoneyChangeMemID,MoneyChangeUserID,MoneyChangeType,MoneyChangeAccount,MoneyChangeMoney,MoneyChangeCash,MoneyChangeBalance,MoneyChangeUnionPay,MemMoney,MoneyChangeCreateTime,MoneyChangeGiveMoney)");
            builder.Append(" values (");
            builder.Append("@MoneyChangeMemID,@MoneyChangeUserID,@MoneyChangeType,@MoneyChangeAccount,@MoneyChangeMoney,@MoneyChangeCash,@MoneyChangeBalance,@MoneyChangeUnionPay,@MemMoney,@MoneyChangeCreateTime,@MoneyChangeGiveMoney)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeMemID", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeUserID", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeType", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeAccount", SqlDbType.NVarChar, 50), new SqlParameter("@MoneyChangeMoney", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeCash", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeBalance", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeUnionPay", SqlDbType.Money, 8), new SqlParameter("@MemMoney", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeCreateTime", SqlDbType.DateTime), new SqlParameter("@MoneyChangeGiveMoney", SqlDbType.Money, 8) };
            cmdParms[0].Value = model.MoneyChangeMemID;
            cmdParms[1].Value = model.MoneyChangeUserID;
            cmdParms[2].Value = model.MoneyChangeType;
            cmdParms[3].Value = model.MoneyChangeAccount;
            cmdParms[4].Value = model.MoneyChangeMoney;
            cmdParms[5].Value = model.MoneyChangeCash;
            cmdParms[6].Value = model.MoneyChangeBalance;
            cmdParms[7].Value = model.MoneyChangeUnionPay;
            cmdParms[8].Value = model.MemMoney;
            cmdParms[9].Value = model.MoneyChangeCreateTime;
            cmdParms[10].Value = model.MoneyChangeGiveMoney;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MoneyChangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MoneyChangeLog ");
            builder.Append(" where MoneyChangeID=@MoneyChangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MoneyChangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MoneyChangeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MoneyChangeLog ");
            builder.Append(" where MoneyChangeID in (" + MoneyChangeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MoneyChangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MoneyChangeLog");
            builder.Append(" where MoneyChangeID=@MoneyChangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MoneyChangeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string orderaccount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MoneyChangeLog");
            builder.Append(" where MoneyChangeAccount=@MoneyChangeAccount");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeAccount", SqlDbType.VarChar, 20) };
            cmdParms[0].Value = orderaccount;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MoneyChangeID,MoneyChangeMemID,MoneyChangeUserID,MoneyChangeType,MoneyChangeAccount,MoneyChangeMoney,MoneyChangeCash,MoneyChangeBalance,MoneyChangeUnionPay,MemMoney,MoneyChangeCreateTime,MoneyChangeGiveMoney ");
            builder.Append(" FROM MoneyChangeLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MoneyChangeID,MoneyChangeMemID,MoneyChangeUserID,MoneyChangeType,MoneyChangeAccount,MoneyChangeMoney,MoneyChangeCash,MoneyChangeBalance,MoneyChangeUnionPay,MemMoney,MoneyChangeCreateTime,MoneyChangeGiveMoney ");
            builder.Append(" FROM MoneyChangeLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MoneyChangeID desc");
            }
            builder.Append(")AS Row, T.*  from MoneyChangeLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.MoneyChangeLog GetModel(int MoneyChangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MoneyChangeID,MoneyChangeMemID,MoneyChangeUserID,MoneyChangeType,MoneyChangeAccount,MoneyChangeMoney,MoneyChangeCash,MoneyChangeBalance,MoneyChangeUnionPay,MemMoney,MoneyChangeCreateTime,MoneyChangeGiveMoney from MoneyChangeLog ");
            builder.Append(" where MoneyChangeID=@MoneyChangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MoneyChangeID;
            Chain.Model.MoneyChangeLog log = new Chain.Model.MoneyChangeLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeID"] != null) && (set.Tables[0].Rows[0]["MoneyChangeID"].ToString() != ""))
            {
                log.MoneyChangeID = int.Parse(set.Tables[0].Rows[0]["MoneyChangeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeMemID"] != null) && (set.Tables[0].Rows[0]["MoneyChangeMemID"].ToString() != ""))
            {
                log.MoneyChangeMemID = int.Parse(set.Tables[0].Rows[0]["MoneyChangeMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeUserID"] != null) && (set.Tables[0].Rows[0]["MoneyChangeUserID"].ToString() != ""))
            {
                log.MoneyChangeUserID = int.Parse(set.Tables[0].Rows[0]["MoneyChangeUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeType"] != null) && (set.Tables[0].Rows[0]["MoneyChangeType"].ToString() != ""))
            {
                log.MoneyChangeType = int.Parse(set.Tables[0].Rows[0]["MoneyChangeType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeAccount"] != null) && (set.Tables[0].Rows[0]["MoneyChangeAccount"].ToString() != ""))
            {
                log.MoneyChangeAccount = set.Tables[0].Rows[0]["MoneyChangeAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeMoney"] != null) && (set.Tables[0].Rows[0]["MoneyChangeMoney"].ToString() != ""))
            {
                log.MoneyChangeMoney = decimal.Parse(set.Tables[0].Rows[0]["MoneyChangeMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeCash"] != null) && (set.Tables[0].Rows[0]["MoneyChangeCash"].ToString() != ""))
            {
                log.MoneyChangeCash = decimal.Parse(set.Tables[0].Rows[0]["MoneyChangeCash"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeBalance"] != null) && (set.Tables[0].Rows[0]["MoneyChangeBalance"].ToString() != ""))
            {
                log.MoneyChangeBalance = decimal.Parse(set.Tables[0].Rows[0]["MoneyChangeBalance"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeUnionPay"] != null) && (set.Tables[0].Rows[0]["MoneyChangeUnionPay"].ToString() != ""))
            {
                log.MoneyChangeUnionPay = decimal.Parse(set.Tables[0].Rows[0]["MoneyChangeUnionPay"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemMoney"] != null) && (set.Tables[0].Rows[0]["MemMoney"].ToString() != ""))
            {
                log.MemMoney = decimal.Parse(set.Tables[0].Rows[0]["MemMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeCreateTime"] != null) && (set.Tables[0].Rows[0]["MoneyChangeCreateTime"].ToString() != ""))
            {
                log.MoneyChangeCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["MoneyChangeCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MoneyChangeGiveMoney"] != null) && (set.Tables[0].Rows[0]["MoneyChangeGiveMoney"].ToString() != ""))
            {
                log.MoneyChangeGiveMoney = decimal.Parse(set.Tables[0].Rows[0]["MoneyChangeGiveMoney"].ToString());
            }
            return log;
        }

        public DataSet GetMoneyChange(string strSql)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select isnull(sum(MoneyChangeMoney),0) as MoneyChangeMoney, isnull(sum(MoneyChangeCash),0) as MoneyChangeCash ,isnull(sum(MoneyChangeBalance),0) as MoneyChangeBalance ,isnull(sum(MoneyChangeUnionPay),0) as MoneyChangeUnionPay,isnull(sum(MoneyChangeGiveMoney),0) as MoneyChangeGiveMoney ");
            builder.Append(" from MoneyChangeLog,Mem,SysShop,SysUser ");
            if (strSql.Trim() != "")
            {
                builder.Append(" where " + strSql);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMoneyChangeLog(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "MoneyChangeLog,Mem,SysUser,SysShop";
            string[] column = new string[] { "MoneyChangeLog.*,Mem.MemCard,Mem.MemName,SysUser.UserName,SysShop.ShopName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "MoneyChangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MoneyChangeLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MoneyChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MoneyChangeLog set ");
            builder.Append("MoneyChangeMemID=@MoneyChangeMemID,");
            builder.Append("MoneyChangeUserID=@MoneyChangeUserID,");
            builder.Append("MoneyChangeType=@MoneyChangeType,");
            builder.Append("MoneyChangeAccount=@MoneyChangeAccount,");
            builder.Append("MoneyChangeMoney=@MoneyChangeMoney,");
            builder.Append("MoneyChangeCash=@MoneyChangeCash,");
            builder.Append("MoneyChangeBalance=@MoneyChangeBalance,");
            builder.Append("MoneyChangeUnionPay=@MoneyChangeUnionPay,");
            builder.Append("MemMoney=@MemMoney,");
            builder.Append("MoneyChangeCreateTime=@MoneyChangeCreateTime,");
            builder.Append("MoneyChangeGiveMoney=@MoneyChangeGiveMoney");
            builder.Append(" where MoneyChangeID=@MoneyChangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MoneyChangeMemID", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeUserID", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeType", SqlDbType.Int, 4), new SqlParameter("@MoneyChangeAccount", SqlDbType.NVarChar, 50), new SqlParameter("@MoneyChangeMoney", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeCash", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeBalance", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeUnionPay", SqlDbType.Money, 8), new SqlParameter("@MemMoney", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeCreateTime", SqlDbType.DateTime), new SqlParameter("@MoneyChangeGiveMoney", SqlDbType.Money, 8), new SqlParameter("@MoneyChangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MoneyChangeMemID;
            cmdParms[1].Value = model.MoneyChangeUserID;
            cmdParms[2].Value = model.MoneyChangeType;
            cmdParms[3].Value = model.MoneyChangeAccount;
            cmdParms[4].Value = model.MoneyChangeMoney;
            cmdParms[5].Value = model.MoneyChangeCash;
            cmdParms[6].Value = model.MoneyChangeBalance;
            cmdParms[7].Value = model.MoneyChangeUnionPay;
            cmdParms[8].Value = model.MemMoney;
            cmdParms[9].Value = model.MoneyChangeCreateTime;
            cmdParms[10].Value = model.MoneyChangeGiveMoney;
            cmdParms[11].Value = model.MoneyChangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

